package services

import (
	"fmt"
	"github.com/GoAdminGroup/go-admin/template/types"
)

// GetActivities GetActivities 获取全部活动
func GetActivities() (results []map[string]interface{}) {
	results, err := GetDb().Query("select * from activities")
	if err != nil {
		return
	}
	return
}

func GetActivityCount() int {
	return len(GetActivities())
}

func GetActivitiesTable() []map[string]types.InfoItem {

	var tableData []map[string]types.InfoItem
	query, err := GetDb().Query("SELECT\n\tc.`name`,\n\ta.title,\n\ta.start_date,\n\ta.end_date, a.created_at FROM\n\tactivities AS a\n\tJOIN clubs AS c ON a.club_id = c.id ORDER BY a.start_date asc  limit 10")
	if err != nil {
		fmt.Println(err)
		return tableData
	}

	for i, _ := range query {
		item := map[string]types.InfoItem{
			"社团名称": {Content: "<span class=\"label label-info\">" + TansTmp(query[i]["name"]) + "</span>"},
			"活动名称": {Content: TansTmp(query[i]["title"])},
			"活动时间": {Content: TansTmp(fmt.Sprintf("%s-%s", query[i]["start_date"], query[i]["end_date"]))},
			"创建时间": {Content: TansTmp(query[i]["created_at"])},
		}
		tableData = append(tableData, item)
	}
	return tableData
}
